package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.TableItem;
import smartclass.entity.TestStudentInfo;
import smartclass.entity.TopicInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TopicDataDao {

	static TopicDataDao topicDao;
	ManagerDataBaseHelper db;
	String tableName;

	public TopicDataDao() {
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.TOPIC_TABLE;
	}

	public static TopicDataDao getInstance() {
		if (topicDao == null) {
			topicDao = new TopicDataDao();
		}
		return topicDao;
	}

	// Add Item
	public boolean addTopic(TopicInfo topic) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");

		ContentValues value = new ContentValues();
		value.put(TableItem.ID, topic.getId());
		value.put(TableItem.NAME, topic.getName());
		value.put(TableItem.DATE_CREAT, topic.getDateCreate());
		value.put(TableItem.PATH, topic.getPath());
		value.put(TableItem.ID_GIAO_VIEN, topic.getIdTeacher());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public TopicInfo getTopic(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		TopicInfo topicInfo = new TopicInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			topicInfo.setId(Integer.parseInt(c.getString(0)));
			topicInfo.setName(c.getString(1));
			topicInfo.setDateCreate(c.getString(2));
			topicInfo.setPath(c.getString(3));
			topicInfo.setIdTeacher(Integer.parseInt(c.getString(4)));
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return topicInfo;
	}

	// Get All Item
	public ArrayList<TopicInfo> getAllTopic() {
		ArrayList<TopicInfo> allTopic = new ArrayList<TopicInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = db.getReadableDatabase().rawQuery(sql, null);
		TopicInfo topicInfo = null;
		if (c.moveToFirst()) {
			do {
				topicInfo = new TopicInfo();
				topicInfo.setId(Integer.parseInt(c.getString(0)));
				topicInfo.setName(c.getString(1));
				topicInfo.setDateCreate(c.getString(2));
				topicInfo.setPath(c.getString(3));
				topicInfo.setIdTeacher(Integer.parseInt(c.getString(4)));
			} while (c.moveToNext());
		}
		return allTopic;
	}

	// Edit Item

	public boolean updateTopic(TopicInfo topic) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues value = new ContentValues();
		value.put(TableItem.ID, topic.getId());
		value.put(TableItem.NAME, topic.getName());
		value.put(TableItem.DATE_CREAT, topic.getDateCreate());
		value.put(TableItem.PATH, topic.getPath());
		value.put(TableItem.ID_GIAO_VIEN, topic.getIdTeacher());
		data.update(tableName, value, TableItem.ID + "=?",
				new String[] { String.valueOf(topic.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + TableItem.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
